import * as inventoryService from '../../services/inventory';
import {queryAssetType} from '../../services/assetType';
import {qryAllUser} from '../../services/user';
import {queryRoomList,qryRackTree} from '../../services/tRoom';
import moment from 'moment'

export default {
  namespace: 'inventory',
  state: {
    list: [],  
    roomList:[], 
    userList:[],  
    rackList:[],
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    viewModalType:'',
    modalType : 'create',
    newKey : '', 
    inventoryId:'',
    currentItem: {}, 
    currentViewItem:{},
    isShowUser:2,
    inventoryName:"",
    modalViewVisible:false,
    detailList:[],
    assetCurrent:1,
    assetCurrentSize : 10,
    assetTotal : 0, 
    currentSheet : 1,
    currentSizeSheet : 10,
    totalSheet : 0, 
    cycletime:{
      cycleType: 1,
      inventoryTimes:[],
    },

  },
  reducers: {
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },        
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },    
    qryRackListSuc(state, { payload: rackList }) {
      return { ...state, rackList };
    },
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, cycletime:{cycleType: 1,inventoryTimes:[], },newKey : new Date().getTime() + '' }
    },
    showViewModal (state, { payload }) {
        return { ...state, ...payload,  modalViewVisible: true }
    },
    hideViewModal (state) {
      return { ...state, modalViewVisible: false, }
    },
    showViewSheetModal (state, { payload }) {
      return { ...state, ...payload,  modalViewSheetVisible: true }
    },
    hideViewSheetModal (state) {
      return { ...state, modalViewSheetVisible: false, }
    },
    onRadioChange (state, { payload }) {
      return { ...state, isShowUser:payload}
    },
    searchAssetSuc(state, { payload }) {
      return { ...state, ...payload };
    },   
    searchSheetSuc(state, { payload }) {
      return { ...state, ...payload };
    },   
    upState(state, { payload}) {
      return { ...state, ...payload };
    },
    
  },
  effects: {    
    *queryUser({ payload },{ call, put }) {    
      const { body }  = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    }, 
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(inventoryService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *qryRackList({ payload },{ call, put }) {     
      const { body }  = yield call(qryRackTree,payload);
      if(body.httpCode==200){
          if(body.data.length>0){
           var array = body.data[0].rackList;
           if(array.length>0){
              yield put({ type: 'qryRackListSuc', payload:body.data});
           }else{
              yield put({ type: 'qryRackListSuc', payload:[]});
           }
          
          }
      }
      
    },    
    *create({ payload },{ call, put }){
      const { body } = yield call(inventoryService.create,payload);
       yield put({ type: 'hideModal'});
       yield put({ type: 'reload' ,payload:{}});
    },
    *qryById({ payload },{ call, put }){
      const { body } = yield call(inventoryService.qryById,payload.id_);
       yield put({ type: 'qryRackList' ,payload:payload.roomId_});
       if(body){
        var list = [];
        body.data.inventoryTimes.map(d=>{
          var dr ={
            ...d,
          };
         dr.startTime = new moment(dr.startTime,"HH:mm");
         list.push(dr);
        });
       yield put({
         type: 'showModal',
         payload: {
           currentItem: body.data,
           modalType: 'edit',
           title: '编辑盘点计划',
           isShowUser:body.data.inventoryType,
           cycletime:{
             cycleType: body.data.cycleType,
             inventoryTimes:list,
           },
         },
      });
      }

    },  
    *onViewItem({ payload },{ call, put }){
      const { body } = yield call(inventoryService.onViewItem,payload);
      yield put({ type: 'searchAsset',payload:{sheetId:payload,status:""}});
      yield put({
          type: 'showViewModal',
          payload: {
            currentViewItem: body.data,
            viewModalType: 'view',
            title: '查看盘点详情',
          },
      });
    },   

    *onViewSheet({ payload,name },{ call, put }){
      const { body } = yield call(inventoryService.onViewSheet,payload);
      yield put({ 
        type: 'showViewSheetModal', 
        payload: { 
          modalType: 'view',
          inventoryId:payload.inventoryId,
          inventoryName:name,
          listSheet:body.data, 
          currentSheet:body.current, 
          currentSizeSheet:body.size, 
          totalSheet:body.iTotalRecords
        }
      });
    },  
    *searchSheetList({ payload },{ call, put }){
      const { body } = yield call(inventoryService.onViewSheet,payload);
      yield put({ 
        type: 'searchSheetSuc', 
        payload: { 
          modalType: 'view',
          inventoryId:payload.inventoryId,
          listSheet:body.data, 
          currentSheet:body.current, 
          currentSizeSheet:body.size, 
          totalSheet:body.iTotalRecords
        }
      });
    },  

    *searchAsset({ payload },{ call, put }) {
      const { body } = yield call(inventoryService.searchAsset,payload);
      yield put({ type: 'searchAssetSuc', payload: { detailList:body.data, assetCurrent:body.current, assetCurrentSize:body.size, assetTotal:body.iTotalRecords}});
    },
    *edit({ payload },{ call, put }){
      const { body } = yield call(inventoryService.edit,payload);
      yield put({ type: 'hideModal'});
      yield put({ type: 'reload' ,payload:{}});
    }, 
    *remove({ payload },{ call, put }){
      const { body } = yield call(inventoryService.remove,payload.id);
      yield put({ type: 'reload' ,payload:payload.search});
    },  
    *goInventory({ payload },{ call, put }){
      const { body } = yield call(inventoryService.goInventory,payload.id);
      yield put({ type: 'reload' ,payload:payload.search});
    },  
    *reload({ payload },{ call, put }){
       yield put({ type: 'queryList',payload});
    },      
   
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/inventory') {
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryRoom' });          
         
        }
      });
    },
  },
};
